package com.example.administrator.liangcang.viwe;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liangcang.R;

/**
 * Created by Administrator on 2017.2.10.
 */

public class MyPopupWindow {
    private Context context;
    private View view;
    private PopupWindow window;
    private int layOutId;
    private View parent;
    private onClickMyWindow callback;
    private String itemId;
    private int  height ;
    private String[] url = new String[]{"","goods_sum", "followers","action_time","reg_time"};

    public void setCallback(onClickMyWindow callback) {
        this.callback = callback;
    }

    public MyPopupWindow(final Context context, int layOutId, View parent,int  height) {
        this.context = context;
        this.layOutId = layOutId;
        this.parent = parent;
        this.height =height;
        view = LayoutInflater.from(context).inflate(layOutId, null, false);
        window = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, height, true);
        isThisItem(view);
        aboutListener();
    }

    private void isThisItem(View v) {
        switch (v.getId()) {
            case R.id.daren_pop:
                RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.daren_pop_group);
                radioGroupListener(radioGroup);
                break;
        }
    }

    private void radioGroupListener(RadioGroup group) {
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                window.dismiss();
                callback.onClickMyWindowListener(true);
                switch (checkedId) {
                    case R.id.daren_pop_button1:
                        itemId = url[0];
                        break;
                    case R.id.daren_pop_button2:
                        itemId = url[1];
                        break;
                    case R.id.daren_pop_button3:
                        itemId = url[2];
                        break;
                    case R.id.daren_pop_button4:
                        itemId = url[3];
                        break;
                    case R.id.daren_pop_button5:
                        itemId = url[4];
                        break;
                }
                callback.onClickMyWindowButtonListener(itemId);
            }
        });
    }

    public void showWindow() {
        window.showAsDropDown(parent, 0, 20);
    }

    private void aboutListener() {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (window != null && window.isShowing()) {
                    window.dismiss();
                    callback.onClickMyWindowListener(true);
                }
                return false;
            }
        });
    }

    public interface onClickMyWindow {
        void onClickMyWindowListener(boolean isClose);

        void onClickMyWindowButtonListener(String id);
    }

}
